package com.lingnan.wanxiangxinxun.advice;

import com.lingnan.wanxiangxinxun.entity.vo.common.Result;
import org.springframework.validation.BindException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(BindException.class) //BindException在参数验证失败时抛出
    public Result<String> handleBindException(BindException e) {
        e.printStackTrace();
        String errorMessage = e.getBindingResult().getAllErrors().get(0).getDefaultMessage(); // 提取第一个错误信息
        return Result.error(errorMessage);
    }

//    @ExceptionHandler(Exception.class)
//    public Result<String> handlerException(Exception e) {
//        e.printStackTrace();
//        return Result.error(e.getMessage());
//    }

}
